package org.adorsys.aderp.orga.web;

import java.util.Collections;

import org.adorsys.aderp.orga.domain.OrgaPerson;
import org.adorsys.aderp.orga.domain.OrgaRelation;
import org.adorsys.aderp.orga.domain.OrgaRelationRole;
import org.springframework.data.domain.Page;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@RequestMapping("/orgarelations")
@Controller
@RooWebScaffold(path = "orgarelations", formBackingObject = OrgaRelation.class)
public class OrgaRelationController {

	
    @RequestMapping(params = "form", produces = "text/html")
    public String createForm(@RequestParam(value="personId", required=false)Long personId, 
    		@RequestParam(value="targetPersonId", required=false)Long targetPersonId,
    		@RequestParam(value="relationRoleId", required=false)Long relationRoleId,
    		Model uiModel) {
    	populateCreateForm(uiModel, new OrgaRelation(), personId,targetPersonId,relationRoleId);
        return "orgarelations/create";
    }
    

    @RequestMapping(produces = "text/html")
    public String list(@RequestParam(value="personId", required=false)Long personId, 
    		@RequestParam(value="targetPersonId", required=false)Long targetPersonId,
    		@RequestParam(value="relationRoleId", required=false)Long relationRoleId,
    		@RequestParam(value = "page", required = false, defaultValue="0") Integer page, 
    		@RequestParam(value = "size", required = false, defaultValue="10") Integer size, Model uiModel) {
    	
		OrgaPerson person = personId!=null? orgaPersonService.findOrgaPerson(personId):null;
		OrgaPerson targetPerson = targetPersonId!=null? orgaPersonService.findOrgaPerson(targetPersonId):null;
		OrgaRelationRole relationRole = relationRoleId!=null? orgaRelationRoleService.findOrgaRelationRole(relationRoleId):null;

    	Page<OrgaRelation> personPage = orgaRelationService.searchOrgaRelations(person, targetPerson, relationRole,page, size);
        uiModel.addAttribute("orgarelations", personPage.getContent());
        uiModel.addAttribute("maxPages", personPage.getTotalPages());

        addDateTimeFormatPatterns(uiModel);

        return "orgarelations/list";
    }    
    
    void populateCreateForm(Model uiModel, OrgaRelation orgaRelation, 
    		Long personId,Long targetPersonId,Long relationRoleId) {
		OrgaPerson person = personId!=null? orgaPersonService.findOrgaPerson(personId):null;
		OrgaPerson targetPerson = targetPersonId!=null? orgaPersonService.findOrgaPerson(targetPersonId):null;
		OrgaRelationRole relationRole = relationRoleId!=null? orgaRelationRoleService.findOrgaRelationRole(relationRoleId):null;

		orgaRelation.setPerson(person);
		orgaRelation.setTargetPerson(targetPerson);
		orgaRelation.setRelationRole(relationRole);

		uiModel.addAttribute("orgaRelation", orgaRelation);
        addDateTimeFormatPatterns(uiModel);
        
        if(person!=null){
        	uiModel.addAttribute("people", Collections.singleton(person));        	
        } else {
        	uiModel.addAttribute("people", orgaPersonService.findAllOrgapeople());        	
        }
        
        if(targetPerson!=null){
        	uiModel.addAttribute("targetpeople", Collections.singleton(targetPerson));        	
        } else {
        	uiModel.addAttribute("targetpeople", orgaPersonService.findAllOrgapeople());        	
        }

        if(relationRole!=null){
        	uiModel.addAttribute("orgarelationroles", Collections.singleton(relationRole));        	
        } else {
            uiModel.addAttribute("orgarelationroles", orgaRelationRoleService.findAllOrgaRelationRoles());
        }
    }
}
